package main;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import pojo.Author;
import pojo.Book;
import pojo.Publisher;
import pojo.Translator;

import com.updatedatasourcesessionbean.*;

import dao.AuthorDAO;
import dao.BookDAO;
import dao.PublisherDAO;
import dao.TranslatorDAO;


public class SyncData {
	/*public void sync() throws NamingException, SQLException {
		final String sql = "select * from author";
		Properties properties = new Properties();
		properties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");
		InitialContext ic = new InitialContext(properties);
		
		DataSource ds = (DataSource) ic.lookup("java:jboss/datasources/MySqlDS");
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = ds.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()) {
				//System.out.println("Query '" + sql + "' returned " + rs.getString(1));
				System.out.println("abc!");
			}
		} finally {
			if(rs != null) rs.close();
			if(stmt != null) stmt.close();
			if(con != null) con.close();
		}
	}*/
	public void sync(){
		//System.out.println("1212121");
		 
		//System.out.println(newAuthors.size());
		
		UpdateDatasourceRemote proxy = null;
		try {
			proxy = lookupUpdateDatasourceRemote();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<Integer> ids = new ArrayList<Integer>();
		List<String> names = new ArrayList<String>();
		
		//sync author table
		AuthorDAO authordao = new AuthorDAO();
		List<Author> newAuthors = authordao.getAuthorByIsNew();
		for(Author a : newAuthors){
			ids.add(a.getAuthorId());
			names.add(a.getName());
		}
		proxy.updateAuthorTable(ids, names);
		ids.clear();
		names.clear();
		//sync publisher table
		PublisherDAO publisherdao = new PublisherDAO();
		List<Publisher> newPublishers = publisherdao.getPublisherByIsNew();
		for(Publisher p : newPublishers){
			ids.add(p.getPublisherId());
			names.add(p.getName());
		}
		proxy.updatePublisherTable(ids, names);
		ids.clear();
		names.clear();
		//sync translator table
		TranslatorDAO translatordao = new TranslatorDAO();
		List<Translator> newTranslators = translatordao.getTranslatorByIsNew();
		for(Translator t : newTranslators){
			ids.add(t.getTranslatorId());
			names.add(t.getName());
		}
		proxy.updateTranslatorTable(ids, names);
		ids.clear();
		names.clear();
		//sync book table
		List<Integer> authorIds = new ArrayList<Integer>();
		List<Integer> translatorIds = new ArrayList<Integer>();
		List<Integer> publisherIds = new ArrayList<Integer>();
		List<Integer> pageNums = new ArrayList<Integer>(); 
		List<Float> widths = new ArrayList<Float>();
		List<Float> heights = new ArrayList<Float>();
		List<Float> weights = new ArrayList<Float>();
		List<Double> prices = new ArrayList<Double>();
		List<Date> publishDays = new ArrayList<Date>();
		List<String> imageUrls = new ArrayList<String>();
		List<String> contents = new ArrayList<String>();
		List<String> bookUrls = new ArrayList<String>();
		List<Integer> topicIds = new ArrayList<Integer>();
		BookDAO bookdao = new BookDAO();
		List<Book> newBooks = bookdao.getBookByIsNew();
		for(Book b : newBooks){
			ids.add(b.getBookId());
			names.add(b.getName());
			authorIds.add(b.getAuthor().getAuthorId());
			translatorIds.add(b.getTranslator().getTranslatorId());
			publisherIds.add(b.getPublisher().getPublisherId());
			pageNums.add(b.getPageNum());
			widths.add(b.getWidth());
			heights.add(b.getHeight());
			weights.add(b.getWeight());
			prices.add(b.getPrice());
			publishDays.add(b.getPublishDay());
			imageUrls.add(b.getImageUrl());
			contents.add(b.getContent());
			bookUrls.add(b.getBookUrl());
			topicIds.add(b.getTopic().getTopicId());
		}
		proxy.updateBookTable(ids, names, authorIds, translatorIds, publisherIds, pageNums, widths, heights, weights, prices, publishDays, imageUrls, contents, bookUrls, topicIds);
		ids.clear();
		names.clear();
		authorIds.clear();
		translatorIds.clear();
		publisherIds.clear();
		pageNums.clear();
		widths.clear();
		heights.clear();
		weights.clear();
		prices.clear();
		publishDays.clear();
		imageUrls.clear();
		contents.clear();
		bookUrls.clear();
		topicIds.clear();
	}
	private UpdateDatasourceRemote lookupUpdateDatasourceRemote() throws NamingException{
		 final Hashtable jndiProperties = new Hashtable();
	     jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
	     final Context context = new InitialContext(jndiProperties);
	     //final String appName = "";
	     //final String moduleName = "UpdateDatasource";
	     //final String distinctName = "";
	     //final String beanName = UpdateDatasourceBean.class.getSimpleName();
	     //final String viewClassName = UpdateDatasourceRemote.class.getName();
	     return (UpdateDatasourceRemote)context.lookup("ejb:/UpdateDatasource//UpdateDatasourceBean!com.updatedatasourcesessionbean.UpdateDatasourceRemote");
	}
}
